import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/dms_documenttype_odoo_api.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/constants/constant.dart';
import 'package:flutter_admin/models/odoo/dms_documenttype.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/utils/store_util.dart';
import '../../../generated/l10n.dart';

class DmsDocumenttypeEdit extends StatefulWidget {
  DmsDocumenttypeEdit({this.dmsDocumenttype});

  final DmsDocumenttype? dmsDocumenttype;

  @override
  _DmsDocumenttypeEditState createState() => _DmsDocumenttypeEditState();
}

class _DmsDocumenttypeEditState extends State<DmsDocumenttypeEdit> {
  late DmsDocumenttype dmsDocumenttype;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  List<SelectOptionVO> departmentList = [];
  @override
  void initState() {
    super.initState();
    this.isAdd = widget.dmsDocumenttype == null;
    this.dmsDocumenttype = widget.dmsDocumenttype ?? DmsDocumenttype();
    var deparment =
        StoreUtil.read(Constant.KEY_CURRENT_USER_INFO)[0]["department_id"];
    dmsDocumenttype.departmentId ??= deparment[0].toString();
    dmsDocumenttype.departmentName ??= deparment[1].toString();

    _loadSelectVO();
  }

  _loadSelectVO() async {
    ResponseBodyApi responseBodyApi = await DynamicDataApi.getModelById(
        model: "hr.department", id: dmsDocumenttype.departmentId);
    if (responseBodyApi.success!) {
      dmsDocumenttype.departmentcode = responseBodyApi.data['department_code'];
    }
    departmentList = await DynamicDataApi.getModelSelect(
      model: "hr.department",
      name: "complete_name",
    );
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: '档案项目代码',
            value: dmsDocumenttype.documenttypecode,
            onSaved: (v) {
              dmsDocumenttype.documenttypecode = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '档案项目名称',
            value: dmsDocumenttype.documenttypename,
            onSaved: (v) {
              dmsDocumenttype.documenttypename = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          // CryInput(
          //   label: '所属部门',
          //   value: dmsDocumenttype.departmentId,
          //   onSaved: (v) {
          //     dmsDocumenttype.departmentId = v;
          //   },
          //   validator: (v) {
          //     return v!.isEmpty ? S.of(context).required : null;
          //   },
          // ),
          CrySelect(
            label: '部门',
            dataList: departmentList,
            value: dmsDocumenttype.departmentId,
            onSaved: (v) {
              dmsDocumenttype.departmentId = v;
            },
            onChange: (v) async {
              if (dmsDocumenttype.departmentId != v) {
                formKey.currentState!.save();
                dmsDocumenttype.departmentId = v;
                if (v != "") {
                  try {
                    ResponseBodyApi responseBodyApi =
                        await DynamicDataApi.getModelById(
                            model: "hr.department",
                            id: dmsDocumenttype.departmentId);
                    if (responseBodyApi.success!) {
                      dmsDocumenttype.departmentcode =
                          responseBodyApi.data['department_code'];
                    }
                  } catch (e) {
                    dmsDocumenttype.departmentcode = "";
                  }
                } else {
                  dmsDocumenttype.departmentcode = "";
                }
                setState(() {});
              }
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '部门代码',
            value: dmsDocumenttype.departmentcode,
            onSaved: (v) => {dmsDocumenttype.departmentcode = v},
            enable: false,
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          // CrySelect(
          //   label: '部门代码',
          //   dataList: departmentcodeList,
          //   value: dmsDocumenttype.departmentcode,
          //   onSaved: (v) {
          //     dmsDocumenttype.departmentcode = v;
          //   },
          //   validator: (v) {
          //     return v!.isEmpty ? S.of(context).required : null;
          //   },
          // ),
          CryInputNum(
            label: '保存期限(年)',
            value: dmsDocumenttype.shelflife,
            onSaved: (v) {
              dmsDocumenttype.shelflife = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '归档责任人',
            value: dmsDocumenttype.saveperson,
            onSaved: (v) {
              dmsDocumenttype.saveperson = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '范围',
            value: dmsDocumenttype.range,
            onSaved: (v) {
              dmsDocumenttype.range = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            DmsDocumenttypeOdooApi.saveOrUpdate(dmsDocumenttype.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                CryUtils.message(res.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
